﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace com.orange.examination
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["admin"] == null)
            {
                Response.Redirect("../LoginAndRegister/Login.aspx");
            }
            if (!IsPostBack)
            {
                string sql = "select LessonName  from tb_Lesson";
                DataSet ds = BaseClass.ExecDataSet(sql);
                ddlTeacherKm.DataSource = ds.Tables[0];
                ddlTeacherKm.DataTextField = "LessonName";
                ddlTeacherKm.DataBind();
                this.ddlTeacherKm.Items.Insert(0, new ListItem("--请选择--"));
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtTeacherName.Text == "" || txtTeacherNum.Text == "" || txtTeacherPwd.Text == "")
            {
                Response.Write("<script type=text/javascript>alert('请将信息填写完整！')</script>");
                return;
            }
            else
            {
                MySqlConnection conn = BaseClass.DBCon();
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("select count(*) from tb_Teacher where TeacherNum='" + txtTeacherNum.Text.Trim() + "'", conn);
                int t = Convert.ToInt32(cmd.ExecuteScalar());
                if (t > 0)
                {
                    Response.Write("<script type=text/javascript>alert('此教师编号已经存在！')</script>");
                    return;
                }
                else
                {
                    string str = "insert into tb_Teacher(TeacherNum,TeacherName,TeacherPwd,TeacherCourse) values('" + txtTeacherNum.Text.Trim() + "','" + txtTeacherName.Text.Trim() + "','" + txtTeacherPwd.Text.Trim() + "','" + ddlTeacherKm.SelectedValue.ToString() + "')";
                    BaseClass.OperateData(str);
                    Response.Write("<script type=text/javascript>alert('教师信息添加成功！')</script>");
                    btnconcel_Click(sender, e);
                }
            }
        }
        protected void btnconcel_Click(object sender, EventArgs e)
        {
            txtTeacherPwd.Text = "";
            txtTeacherNum.Text = "";
            txtTeacherName.Text = "";
        }
    }
}